﻿using RBAC.MaHaiBo.Entity.Traceability;
using RBAC.MaHaiBo.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using RBAC.MaHaiBo.Repository.Interface.TraceabilityInterface;
using RBAC.MaHaiBo.Repository.Interface;

namespace RBAC.MaHaiBo.Service.TraceabilityService
{
    /// <summary>
    /// 屠宰服务层
    /// </summary>
    public class SlaughterService:ISlaughterService
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<SlaughterService> logger;

        /// <summary>
        /// 屠宰
        /// </summary>
        ISlaughterRepository SlaughterR;
        /// <summary>
        /// 企业
        /// </summary>
        IFirmRepository FirmR;
        /// <summary>
        /// 省仓储
        /// </summary>
        IProvinceRepository pRep;
        /// <summary>
        /// 市仓储
        /// </summary>
        ICityRepository cityRep;
        /// <summary>
        /// 县仓储
        /// </summary>
        ICountyRepository couRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="SlaughterR">屠宰</param>
        /// <param name="FirmR">企业</param>
        public SlaughterService(ILogger<SlaughterService> logger, ISlaughterRepository SlaughterR, IFirmRepository FirmR,
            IProvinceRepository pRep,
            ICityRepository cityRep,
            ICountyRepository couRep)
        {
            this.logger = logger;
            this.SlaughterR = SlaughterR;
            this.FirmR = FirmR;
            this.pRep = pRep;
            this.cityRep = cityRep;
            this.couRep = couRep;
        }

        /// <summary>
        /// 添加屠宰信息
        /// </summary>
        /// <param name="slaughter">要添加的数据信息</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> AddSlaughter(Slaughter slaughter)
        {
            try
            {
                return SlaughterR.AddAsync(slaughter);
            }
            catch (Exception ex)
            {
                logger.LogError("【屠宰服务层】 添加屠宰信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改屠宰信息
        /// </summary>
        /// <param name="slaughterId">修改后的屠宰信息</param>
        /// <returns>返回受影响行数 </returns>
       public async Task<int> UPdSlaughter(Slaughter slaughterId) {
            try
            {
                var qout = await SlaughterR.GetModelAsync(slaughterId.slaughterid);
                qout.LOT= slaughterId.LOT;
                qout.Slaughtertime = slaughterId.Slaughtertime;
                qout.Arrivaltime = slaughterId.Arrivaltime;
                qout.counterbalance = slaughterId.counterbalance;
                return await SlaughterR.UpdateAsync(qout);
            }
            catch (Exception ex)
            {
                logger.LogError("【屠宰服务层】 修改屠宰信息 出错" + ex.Message);
                throw;
            }

        }
        /// <summary>
        /// 显示屠宰信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回数据信息</returns>
       public async Task<List<Slaughter>> GetSlaughter(string? LOT)
        {
            try
            {
                return await SlaughterR.GetAsync(p => p.LOT == LOT);
            }
            catch (Exception ex)
            {
                logger.LogError("【屠宰服务层】 修改屠宰信息 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加屠宰企业信息
        /// </summary>
        /// <param name="firm">企业信息</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> AddSlaughterFirm(Firm firm)
        {
            try
            {
                var list = await FirmR.GetAsync(p => p.LOT == firm.LOT);
                var firmindo = new Firm();
                foreach (var item in list)
                {
                    firmindo = item;
                }
                firmindo.provinceId = firm.provinceId;
                firmindo.cityId = firm.cityId;
                firmindo.countyId = firm.countyId;
                firmindo.address = firm.address;
                firmindo.FirmSynopsis = firm.FirmSynopsis;
                firmindo.SlaughterFirmName = firm.SlaughterFirmName;
                firmindo.EstablishTime = firm.EstablishTime;
                return await FirmR.UpdateAsync(firmindo);
            }
            catch (Exception ex)
            {
                logger.LogError("【屠宰服务层】 添加屠宰企业信息 出错" + ex.Message);
                throw;
            }
           

        }

        /// <summary>
        /// 显示屠宰企业信息
        /// </summary>
        /// <returns>返回数据集合</returns>
       public async Task<List<SlaughterFirmDTO>> GetSlaughterFirm(string? LOT)
        {
            try
            {

                
                var p = await pRep.GetAllAsync();
                var city = await cityRep.GetAllAsync();
                var cou = await couRep.GetAllAsync();


                var list = (from f in await FirmR.GetAllAsync()
                            where f.LOT== LOT
                            select new SlaughterFirmDTO
                            {
                                Firmid = f.Firmid,
                                provinceId = f.provinceId,
                                cityId = f.cityId,
                                countyId = f.countyId,
                                address = f.address,
                                EstablishTime = f.EstablishTime,
                                FirmSynopsis = f.FirmSynopsis,
                                LOT = f.LOT,
                                SlaughterFirmName = f.SlaughterFirmName,
                                FirmName = f.FirmName,
                                ProductionPermit = f.ProductionPermit,
                                farmFirm = f.farmFirm,
                                principal = f.principal,
                                NumberOfFields = f.NumberOfFields,
                                cityName = city.Where(p =>p.cityId == f.cityId).ToList(),
                                countyName = cou.Where(p => p.countyId == f.countyId).ToList(),
                                provinceName =p.Where(p=>p.provinceId== f.provinceId).ToList()
                            });
                return list.ToList();
            }
            catch (Exception ex)
            {
                logger.LogError("【屠宰服务层】 显示屠宰企业信息 出错" + ex.Message);
                throw;
            }
       }

    }
}
